package com.smmisia.article.entity;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Optional;

import com.smmisia.article.bo.CreateGroupBO;
import lombok.Builder;
import lombok.Data;

/**
 * 
 * @TableName chat_group
 */
@TableName(value ="chat_group")
@Data
@Builder
public class Group implements Serializable {
    /**
     * 群id
     */
    @TableId(type = IdType.AUTO)
    private Integer id;

    /**
     * 结伴id
     */
    private Integer accompanyId;

    /**
     * 创建人id
     */
    private Integer userId;

    /**
     * 群组名
     */
    private String groupName;

    /**
     * 群组描述
     */
    private String description;

    /**
     * 创建时间
     */
    private LocalDateTime createTime;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        Group other = (Group) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getAccompanyId() == null ? other.getAccompanyId() == null : this.getAccompanyId().equals(other.getAccompanyId()))
            && (this.getUserId() == null ? other.getUserId() == null : this.getUserId().equals(other.getUserId()))
            && (this.getGroupName() == null ? other.getGroupName() == null : this.getGroupName().equals(other.getGroupName()))
            && (this.getDescription() == null ? other.getDescription() == null : this.getDescription().equals(other.getDescription()))
            && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getAccompanyId() == null) ? 0 : getAccompanyId().hashCode());
        result = prime * result + ((getUserId() == null) ? 0 : getUserId().hashCode());
        result = prime * result + ((getGroupName() == null) ? 0 : getGroupName().hashCode());
        result = prime * result + ((getDescription() == null) ? 0 : getDescription().hashCode());
        result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", accompanyId=").append(accompanyId);
        sb.append(", userId=").append(userId);
        sb.append(", groupName=").append(groupName);
        sb.append(", description=").append(description);
        sb.append(", createTime=").append(createTime);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }

    public static Group create(CreateGroupBO request) {
        return Group.builder()
                .groupName(request.getGroupName())
                .description(request.getDescription())
                .userId(Optional.ofNullable(request.getUserId())
                        .orElseGet(StpUtil::getLoginIdAsInt))
                .accompanyId(request.getAccompanyId())
                .build();
    }
}
